<template>
	<view class="task_giftorder_page">
		<view v-if="addrsinfo.id==0" class="group_public_btn_empty_red" @click="goEditAddr(1)">请添加收货地址</view>
		<view v-if="addrsinfo.id!=0" class="addr_box" @click="goAddrList(index)">
			<uni-icons class="addr_location_icon" type="location" size="24" color="#00A0DC"></uni-icons>
			<view class="flex-1 ml-5 mr-15">
				<view class="display_flex">
					<view class="display_flex flex-1">
						<view class="f-15">{{addrsinfo.name}}</view>
						<view class="f-15 c-666666">{{addrsinfo.mobile}}</view>
						<view v-if="addrsinfo.status==1" class="def_btn ml-5">默认</view>
					</view>
				</view>
				<view class="f-12 c-999999 mt-10">
					{{addrsinfo.area.province_name}}{{addrsinfo.area.city_name}}{{addrsinfo.area.area_name}}{{addrsinfo.address}}
				</view>
			</view>
			<uni-icons type="forward" size="22" color="#00A0DC"></uni-icons>
		</view>
		<view class="f-15 c-999999 mt-25">商品信息</view>
		<view class="giftorder_info_box mt-20">
			<view class="giftorder_img_box">
				<image v-if="info.cover_img" class="giftorder_img" :src="picUrl+info.cover_img"></image>
			</view>
			<view class="giftorder_info">
				<view class="f-13">{{info.name}}</view>
				<view v-if="nowTime" class="f-10 c-999999 mt-10">兑换时间：{{nowTime}}</view>
				<view class="giftorder_price">消耗油滴{{info.price}}L</view>
			</view>
		</view>
		<view class="f-15 c-999999 mt-25">配送信息</view>
		<view class="display_flex f-13 mt-20">
			<view class="flex-1">配送方式</view>
			<view class="c-999999">邮寄</view>
		</view>
		<view class="giftorder_bottom_btn" :style="'bottom:'+btnbottom+'px;'">
			<view class="group_public_btn_block" @click="goodsPay">确认兑换</view>
		</view>
		<view class=""></view>

	</view>
</template>

<script>
	import {
		getDefaultShopAddr,
		shopIntegralPay,
		getGroupUserInfo,
	} from '@/api/motortime.js';
	let app = getApp()
	export default {
		data() {
			return {
				info: '',
				nowTime: '',
				formattedDate: '',
				addrsinfo: '',
				isSubmit: true,
				picUrl: app.globalData.picUrl,
				onlinePic: app.globalData.onlinePic,
				integral: uni.getStorageSync('userinfo') ? uni.getStorageSync('userinfo').integral : 0,
				btnbottom: uni.getSystemInfoSync().safeAreaInsets.bottom ? uni.getSystemInfoSync().safeAreaInsets.bottom +
					10 : 30
			};
		},
		onShow() {
			if (uni.getStorageSync('taskAddrInfo')) {
				this.addrsinfo = uni.getStorageSync('taskAddrInfo') ? uni.getStorageSync('taskAddrInfo') : ''
				uni.removeStorage({
					key: 'taskAddrInfo'
				})
			} else {
				this.getDefaultShopAddr()
			}
			let date = new Date();
			let year = date.getFullYear();
			let month = String(date.getMonth() + 1).padStart(2, '0'); // 月份是从0开始的  
			let day = String(date.getDate()).padStart(2, '0');
			let hours = String(date.getHours()).padStart(2, '0');
			let minutes = String(date.getMinutes()).padStart(2, '0');
			let seconds = String(date.getSeconds()).padStart(2, '0');
			this.nowTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
		},
		onLoad() {
			this.info = uni.getStorageSync('taskGoodsInfo')
			uni.removeStorage({
				key: 'taskGoodsInfo'
			})
		},
		methods: {
			getDefaultShopAddr() {
				getDefaultShopAddr({}).then(res => {
					if (res.state == 1) {
						uni.hideLoading()
						this.addrsinfo = res.data
					} else {
						uni.hideLoading()
						this.loadingTitle = "- 暂无更多 -"
						uni.showToast({
							title: res.error[0],
							mask: true,
							icon: 'none',
							duration: 1500
						})
					}
				})
			},
			goTaskIndex() {
				uni.redirectTo({
					url: '/pages/taskcentre/index'
				})
			},
			goAddrList() {
				uni.navigateTo({
					url: '/pages/my/myaddress'
				})
			},
			goEditAddr(type, index) {
				if (type == 2) {
					uni.setStorage({
						key: 'taskAddrInfo',
						data: this.addrsList[index]
					})
				}
				uni.navigateTo({
					url: '/pages/my/editaddress'
				})
			},

			goodsPay() {
				if (!this.addrsinfo.id) {
					uni.showToast({
						title: '请添加收货地址',
						icon: 'none'
					})
					return false
				}
				if (this.isSubmit) {
					this.isSubmit = false
					shopIntegralPay({
						goods_id: this.info.goods_id,
						number: 1,
						address_id: this.addrsinfo.id
					}).then(res => {
						if (res.state == 1) {
							var order_id = res.data.order_id
							uni.redirectTo({
								url: '/pages/taskcentre/giftorderinfo?order_id=' + order_id + '&isShow=1'
							})
							this.getGroupUserInfo()
						} else {
							this.isSubmit = true
							uni.hideLoading()
							uni.showToast({
								title: res.error[0],
								mask: true,
								icon: 'none',
								duration: 1500
							})
						}
					})
				}
			},
			getGroupUserInfo() {
				getGroupUserInfo({
					user_id: uni.getStorageSync('userinfo') ? uni.getStorageSync('userinfo').user_id : ''
				}).then(res => {
					if (res.state == 1) {
						this.isSubmit = true
						uni.setStorage({
							key: 'userinfo',
							data: res.data.list,
						});
					} else {
						uni.removeStorage({
							key: 'access_token'
						})
					}
				})
			},
		},
		onPullDownRefresh() {
			uni.stopPullDownRefresh()
		},
		onShareAppMessage(res) {
			app.getShareTask()
			return {

			}
		},
	}
</script>

<style lang="scss">
	.task_giftorder_page {
		padding: 50rpx;

		.addr_box {
			display: flex;
			align-items: center;
			padding: 40rpx 0;
			border-bottom: 1rpx solid #E9E9E9;

			.addr_location_icon {
				margin-left: -10rpx;
			}

			.def_btn {
				color: #00A0DC;
				font-size: 20rpx;
				padding: 0rpx 10rpx;
				border-radius: 8rpx;
				border: 1rpx solid #00A0DC;
			}
		}

		.giftorder_info_box {
			display: flex;
			align-items: flex-start;
			padding-bottom: 50rpx;
			border-bottom: 1rpx solid #E9E9E9;

			.giftorder_img_box {
				width: 200rpx;
				height: 200rpx;
				background-color: #F1F1F1;

				.giftorder_img {
					width: 200rpx;
					height: 200rpx;
					vertical-align: middle;
				}
			}

			.giftorder_info {
				flex: 1;
				height: 200rpx;
				position: relative;
				margin-left: 20rpx;

				.giftorder_price {
					color: #00A0DC;
					font-size: 26rpx;
					position: absolute;
					bottom: 0;
					right: 0;
				}
			}
		}

		.task_order_pop {
			position: fixed;
			top: 100rpx;
			left: 50rpx;
			right: 50rpx;
			z-index: 1001;

			.task_pop_img {
				width: 100%;
			}

			.task_pop_btn {
				width: 240rpx;
				color: #999999;
				font-size: 26rpx;
				padding: 16rpx 0;
				text-align: center;
				border-radius: 30rpx;
				border: 2rpx solid #999999;
				position: absolute;
				bottom: 25%;
				left: 0;
				right: 0;
				margin: 0 auto;
			}
		}

		.giftorder_bottom_btn {
			position: fixed;
			left: 30rpx;
			right: 30rpx;
			z-index: 1;
		}
	}
</style>